<?php

namespace app\console\controller;

use think\Url;
use think\Cache;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\AuthGroup as AuthGroupModel;

class AuthGroup extends Controller
{
    /*
     * 权限组列表
     */
    public function index()
    {
        // 获取参数
        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];

        if (!empty($inquire['title'])) $condition['title|description'] = ['like', '%' . $inquire['title'] . '%'];

        $data = (new AuthGroupModel)->where($condition)->order(['id' => 'desc'])->paginate(50);


        return $this->fetch('index', [
            "mate_title" => '用户组列表',
            'data'       => $data
        ]);
    }


    /*
     * 用户组添加
     */
    public function create()
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('AuthGroup');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new AuthGroupModel())->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('AuthGroup/index'));
            }

            fail('操作失败');


        }
        return $this->fetch('create', [
            "mate_title" => '用户组添加',

        ]);

    }


    /*
     * 用户编辑
     */
    public function update($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 验证
            $validate = Loader::validate('AuthGroup');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = (new AuthGroupModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose !== false) {
                win('操作成功', Url::build('AuthGroup/index'));
            }

            fail('操作失败');

        }

        $data = AuthGroupModel::get($id);


        return $this->fetch('create', [
            "mate_title" => '用户组编辑',
            'data'       => $data,

        ]);
    }



    /*
     * 用户删除
     */
    public function destroy($id)
    {

        if (AuthGroupModel::destroy($id)) {
            win('操作成功', Url::build('AuthGroup/index'));
        }

        fail('操作失败');

    }



    /*
     * 用户组权限
     */
    public function authority($id)
    {

        if (Request::instance()->isPost()) {

            if (AuthGroupModel::jurisdiction($id)) {
                win('操作成功', Url::build('AuthGroup/index'));
            } else {
                fail('操作失败');
            }

        }

        $rules = (new AuthGroupModel)->where('id', $id)->value('rules');
        $d = Cache::get('rule');

        if(empty($d)) $rules = '';
        $ruless = [];
        foreach ($d as $k => $v){
            if($v['module'] == '控制台') continue;
            $ruless[$v['module']][] = $v;
        }
        $new_pri = getMenuList(Cache::get('rule'),0,'parenTid');
        return $this->fetch('', [
            "mate_title" => '用户组权限',
            'data'       => $ruless,
            'rules'      => $rules,
            'id'         => $id,
            'new_pri' => $new_pri
        ]);

    }
}